package ${package};

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.zdsoft.eis.frame.action.PageAction;

import ${rootPkg}.entity.${entityClass};
import ${rootPkg}.service.${serviceClass};

import com.opensymphony.xwork2.ModelDriven;


@SuppressWarnings( { "unchecked", "serial" })
public class ${actionClass} extends PageAction implements ModelDriven {

	private ${serviceClass} ${serviceVar};
    private ${entityClass} ${entityVar} = new ${entityClass}();
    private List<${entityClass}> ${entityVar}List;
    private String ${entityVar}Id;
    private String[] ${entityVar}Ids;
    private String unitId;

	private String error() {
		promptMessageDto.setErrorMessage("非法操作！");
		promptMessageDto.setOperateSuccess(false);
        promptMessageDto.addOperation(new String[] { "确定", "${entityVar}Admin-list.action?unitId="+unitId });
		return PROMPTMSG;
	}

	private boolean dataIsValid() {
		return true;
	}
	
	private String listAndReturn() {
		try {
			${entityVar}List = ${serviceVar}.get${entityClass}List(unitId, getPage());
		} catch (Exception e) {
			e.printStackTrace();
			${entityVar}List = new ArrayList();
			addActionError("数据库操作失败");
		}
		return "list${entityClass}";
	}
	
	public String add() {
		return "edit${entityClass}";
	}

	public String list() {
		if (!dataIsValid()) {
			return error();
		}
		return listAndReturn();
	}

	public String saveAdd() {
        if (!dataIsValid()) return error();

        try {
            ${serviceVar}.add${entityClass}(${entityVar});
            addActionMessage("添加成功!");
            return listAndReturn();
        } catch (Exception e) {
			e.printStackTrace();
            addActionError("添加失败，数据库操作错误！");
            return "edit${entityClass}";
        }
	}

	public String delete() {
		if ( null == ${entityVar}Ids || ${entityVar}Ids.length == 0) {
			return error();
		}

		try {
			boolean deleteResult = ${serviceVar}.delete${entityClass}(${entityVar}Ids);
			if (!deleteResult) {
				addActionError("部分删除失败，可能由于记录已经被删除！");
			} else {
				addActionMessage("删除成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("删除失败，由于数据库操作错误！");
		}
		return listAndReturn();
	}

	public String edit() {
		try {
			${entityVar} = ${serviceVar}.get${entityClass}(${entityVar}Id);
			if ( ${entityVar} == null) {
				addActionError("数据不存在！");
				return listAndReturn();
			} else {
				return "edit${entityClass}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("获取失败，数据库操作错误！");
			return listAndReturn();
		}
	}

	public String saveUpdate() {
        try {
            boolean updateResult = ${serviceVar}.update${entityClass}(${entityVar});
            if (updateResult == false) {
                addActionError("更新失败，数据不存在！");
            } else {
                addActionMessage("更新成功！");
            }
            return listAndReturn();
        } catch (Exception e) {
			e.printStackTrace();
            addActionError("更新失败，数据库操作错误！");
            return "edit${entityClass}";
        }
	}

	public Object getModel() {
		return this.${entityVar};
	}
	
    
	
}

